package com.itheima.controller;

import com.itheima.health.entity.Result;
import com.itheima.health.utils.poi.POIUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.resources.MessageConstant;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Api(tags = "预约管理")
@RestController
public class OrderSettingController {
    @Reference
    private OrderSettingService orderSettingService;

    @ApiOperation(value = "预约导入", notes = "预约导入")
    @PostMapping("ordersetting/importOrderSettings")
    public Result importOrderSettings(@RequestParam("excelFile") MultipartFile file) {
        System.out.println(file + "文件路径?");

        try {
            List<String[]> list = POIUtils.readExcel(file);
            // string[0]   日期   string[1]    最大预约数
            orderSettingService.batchOrderSetting(list);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }

    }

    @ApiOperation(value = "查询预约人数与对应日期", notes = "预约日历面板数据导入")
    @GetMapping("ordersetting/findSettingData/{year}/{month}")
    public Result findSettingData(@PathVariable("year") String year,
                                  @PathVariable("month") String month) {
        try {
            Map map = orderSettingService.findSettingData(year, month);

            System.out.println(map);

            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_ORDER_FAIL);
        }
    }


    @ApiOperation(value = "对应日期预约人数设置", notes = "预约日历面板数据设置")
    @PutMapping("ordersetting/updateOrderSettingData/{settingDay}/{number}")
    public Result updateOrderSettingData(@PathVariable("settingDay") String settingDay,
                                         @PathVariable("number") Integer number) {
        System.out.println(settingDay+"???方法执行!");
        try {
            orderSettingService.updataNumByDay(settingDay, number);
            return new Result(true, MessageConstant.EDIT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ORDERSETTING_FAIL);
        }

    }

}